package com.xyebank.ledger.controller;

import com.alibaba.fastjson.JSON;
import com.xyebank.ledger.conf.MigTaskBean;
import com.xyebank.ledger.manage.MigDispatcher;
import com.xyebank.ledger.util.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/data_transfer")
@Api("数据迁移接口")
public class MigController {

	@Autowired
	private MigDispatcher migrateService;

	@Autowired
	MigTaskBean migTaskBean;

	/**
	 * 迁移所有
	 * 
	 * @return
	 */
	@RequestMapping("/migall")
	@ApiOperation(value = "所有产品数据迁移", httpMethod = "POST", notes = "所有产品数据迁移", code = 200)
	@ApiResponses({ @ApiResponse(code = 200, message = "业务成功完成"),
			@ApiResponse(code = 400, message = "表明在请求 API时提供了错误信息"),
			@ApiResponse(code = 500, message = "表明 API请求时,服务器发生了错误") })
	public @ResponseBody Object migall() {
		int num = migrateService.migerateAll();
		Map<String, String> map = new HashMap<>();
		if (num == 1) {
			map.put("success", "数据迁移成功！！！");
		} else {
			map.put("fail", "数据迁移失败！！！");
		}
		return JSON.toJSONString(map);
	}

	/**
	 * 迁移指定数据
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/appointTransfer_data")
	@ApiOperation(value = "指定产品数据迁移", httpMethod = "POST", notes = "指定产品数据迁移", code = 200)
	@ApiResponses({ @ApiResponse(code = 200, message = "业务成功完成"),
			@ApiResponse(code = 400, message = "表明在请求 API时提供了错误信息"),
			@ApiResponse(code = 500, message = "表明 API请求时,服务器发生了错误") })
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "param", dataType = "String", required = true, value = "指定产品数据迁移", defaultValue = "zadd_apply_jry_") })
	public Object appointTransfer(String param) {
		int num = migrateService.dispatch(param);
		Map<String, String> map = new HashMap<>();
		if (num == 1) {
			map.put("success", "数据迁移成功！！！");
		} else {
			map.put("fail", "数据迁移失败！！！");
		}
		return JSON.toJSONString(map);
	}

	/**
	 * 释放锁
	 * 
	 * @param key
	 * @return
	 */
	@RequestMapping("/release_key")
	@ApiOperation(value = "释放指定产品的锁", httpMethod = "POST", notes = "释放指定产品的锁", code = 200)
	@ApiResponses({ @ApiResponse(code = 200, message = "业务成功完成"),
			@ApiResponse(code = 400, message = "表明在请求 API时提供了错误信息"),
			@ApiResponse(code = 500, message = "表明 API请求时,服务器发生了错误") })
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "key", dataType = "String", required = true, value = "释放指定的锁", defaultValue = "zadd_apply_jry_") })
	public Integer releaseKey(String key) {
		migrateService.release(key);
		return 1;
	}

	/**
	 * 指定产品某个时间段内
	 * 
	 * @return
	 */
	@RequestMapping("/by_time")
	@ApiOperation(value = "指定时间的数据迁移", httpMethod = "POST", notes = "指定时间的数据迁移", code = 200)
	@ApiResponses({ @ApiResponse(code = 200, message = "业务成功完成"),
			@ApiResponse(code = 400, message = "表明在请求 API时提供了错误信息"),
			@ApiResponse(code = 500, message = "表明 API请求时,服务器发生了错误") })
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "param", dataType = "String", required = true, value = "指定的数据", defaultValue = "zadd_apply_jry_"),
			@ApiImplicitParam(paramType = "query", name = "startTime", dataType = "String", required = true, value = "开始时间", defaultValue = "2018-02-02 00:00:00"),
			@ApiImplicitParam(paramType = "query", name = "endTime", dataType = "String", required = true, value = "结束时间", defaultValue = "2018-09-02 00:00:00") })
	public Object byTimeTransfer(String param, String startTime, String endTime) {
		Date start = DateUtil.str2Date(startTime);
		Date end = DateUtil.str2Date(endTime);
		int num = migrateService.dispatch(param, start, end);
		Map<String, String> map = new HashMap<>();
		if (num == 1) {
			map.put("success", "数据迁移成功！！！");
		} else {
			map.put("fail", "数据迁移失败！！！");
		}
		return JSON.toJSONString(map);
	}

}